Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@types/joi
Advanced tools
@types/joi provides TypeScript type definitions for the Joi validation library, which is used to validate JavaScript objects and data structures.
Basic Validation
This feature allows you to define a schema and validate an object against it. The schema can include various rules such as string patterns, number ranges, and required fields.
const Joi = require('joi');
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
birth_year: Joi.number().integer().min(1900).max(2013)
});
const { error, value } = schema.validate({ username: 'abc', birth_year: 1994 });
Custom Error Messages
This feature allows you to customize error messages for specific validation rules, making it easier to provide user-friendly feedback.
const schema = Joi.object({
username: Joi.string().min(3).message('Username must be at least 3 characters long')
});
const { error, value } = schema.validate({ username: 'ab' });
if (error) {
console.log(error.details[0].message); // 'Username must be at least 3 characters long'
}
Nested Object Validation
This feature allows you to validate nested objects, ensuring that all required fields within the nested structure are present and valid.
const schema = Joi.object({
user: Joi.object({
username: Joi.string().required(),
password: Joi.string().required()
}).required()
});
const { error, value } = schema.validate({ user: { username: 'abc', password: '123' } });
Array Validation
This feature allows you to validate arrays, including the types of items within the array and constraints on the array itself, such as minimum length.
const schema = Joi.object({
tags: Joi.array().items(Joi.string().min(2)).min(1).required()
});
const { error, value } = schema.validate({ tags: ['node', 'express'] });
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but is often considered more lightweight and has a more modern API. Yup also integrates well with React and Formik for form validation.
AJV (Another JSON Schema Validator) is a JSON schema validator that is very fast and supports JSON Schema draft-07. It is more focused on JSON schema validation and is often used in applications where performance is critical.
Validator is a library of string validators and sanitizers. It is less comprehensive than Joi but is very useful for simple string validation tasks. It is often used in conjunction with other validation libraries.
npm install --save @types/joi
This package contains type definitions for joi (https://github.com/hapijs/joi).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/joi/v13
Additional Details
These definitions were written by Bart van der Schoor https://github.com/Bartvds, Laurence Dougal Myers https://github.com/laurence-myers, Christopher Glantschnig https://github.com/cglantschnig, David Broder-Rodgers https://github.com/DavidBR-SW, Gael Magnan de Bornier https://github.com/GaelMagnan, Rytis Alekna https://github.com/ralekna, Pavel Ivanov https://github.com/schfkt, Youngrok Kim https://github.com/rokoroku, Dan Kraus https://github.com/dankraus, Anjun Wang https://github.com/wanganjun, Rafael Kallis https://github.com/rafaelkallis, Conan Lai https://github.com/aconanlai, Peter Thorson https://github.com/zaphoyd, Will Garcia https://github.com/thewillg, Simon Schick https://github.com/SimonSchick.
FAQs
Stub TypeScript definitions entry for joi, which provides its own types definitions
We found that @types/joi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.